//
//  OrderDetailViewController.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "CollectTableViewCell.h"
#import "CollectModel.h"
#import "ReadingWebViewController.h"

#define URL(a) [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/0-20.html", a]
@interface OrderDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *myDataArr;
@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, strong) UIView *topV;
@property (nonatomic, strong) UILabel *sourceLab;

@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self createTableView];
    [self createData];
}

- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStyleGrouped)];
    self.tableV.frame = CGRectMake(0, -20, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.separatorStyle = NO;
    [self.view addSubview:self.tableV];
    
    UIImageView *imgV = [[UIImageView alloc] init];
    imgV.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3);
    imgV.image = [UIImage imageNamed:@"meitu.jpg"];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, imgV.bounds.size.height / 2 - 50, SCREEN_WIDTH, 50)];
    titleLab.text = self.alias;
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = [UIFont systemFontOfSize:20];
    titleLab.textColor = [UIColor whiteColor];
    self.tableV.tableHeaderView = imgV;
    [imgV addSubview:titleLab];
    
    self.topV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55)];
    [self.view addSubview:self.topV];
    
    UIButton *retutnBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    retutnBut.frame = CGRectMake(10, 10, 40, 40);
    [retutnBut setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:(UIControlStateNormal)];
    if (1 == self.i) {
     [retutnBut addTarget:self action:@selector(returnAction2) forControlEvents:(UIControlEventTouchUpInside)];
    } else {
    [retutnBut addTarget:self action:@selector(returnAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    
    self.sourceLab = [[UILabel alloc] initWithFrame:CGRectMake(0, self.topV.frame.size.height / 2 - 15, self.topV.bounds.size.width, 40)];
    self.sourceLab.textColor = [UIColor whiteColor];
    self.sourceLab.textAlignment = NSTextAlignmentCenter;
    self.sourceLab.font = [UIFont systemFontOfSize:18];
    self.sourceLab.text = self.source;
    NSLog(@"%@", self.source);
    [self.topV addSubview:retutnBut];
    [self.topV addSubview:self.sourceLab];
    
}

#pragma 开始滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.topV.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:scrollView.contentOffset.y /  + 318];
}

- (void)returnAction
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showView" object:nil];
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController popViewControllerAnimated:YES];[[NSNotificationCenter defaultCenter] postNotificationName:@"hideView" object:nil];
}

- (void)returnAction2
{
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showView" object:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myDataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectModel *model = [self.myDataArr objectAtIndex:indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ReadingWebViewController *webVC = [[ReadingWebViewController alloc] init];
    webVC.code = model.postid;
    webVC.i = 1;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController pushViewController:webVC animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectModel *model = [self.myDataArr objectAtIndex:indexPath.row];
    static NSString *cellStr = @"cell";
    CollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[CollectTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellStr];
    }
    cell.titleLab.text = model.title;
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:nil];
    cell.timeLab.text = model.ptime;
    return cell;
}

- (void)createData
{
    [xwzAFNetTool getNetWithURL:URL(self.code) body:nil headFile:nil responStyle:XWZ_JSON success:^(NSURLSessionDataTask *task, id responseObject) {
       
        NSMutableArray *arr = [responseObject objectForKey:self.code];
        self.myDataArr = [CollectModel baseModeWithArr:arr];
        if (self.myDataArr) {
            [self.tableV reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
